/*
 * AdminSession
 * Buisness functions for create, remove update person information
 */
package edu.depaul.se.courseregistration.sessionbean;

import edu.depaul.se.courseregistration.jpa.Address;
import edu.depaul.se.courseregistration.jpa.user.*;
import javax.ejb.LocalBean;
import javax.ejb.Stateless;
import javax.ejb.TransactionAttribute;
import javax.ejb.TransactionAttributeType;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

/**
 *
 * @author Group2
 */
@Stateless
@LocalBean
@TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
public class AdminEJB {

    @PersistenceContext(unitName="CourseRegistrationModulePU")
    private EntityManager em;     
    // Admin business APIs
    
    public void removePerson(int personID)
    {
        Person person = em.createNamedQuery(Person.PERSON_QUERY_BY_ID, Person.class).
        setParameter("personID", personID).getSingleResult();
        em.remove(person);
    }
    

    public Person createPerson(String personType, String strAddress, String city, String state, String zip, String major, String deptName, String firstName, String lastName, String email, int personId) {
        
       Address address = new Address();
       address.setStreetAddr(strAddress);
       address.setCity(city);
       address.setState(state);
       address.setZipCode(zip);
       
       if (personType.equalsIgnoreCase("STUDENT"))
       {
          Student s = new Student();
          s.setAddress(address);
          s.setMajor(major);
          s.setFirstName(firstName);
          s.setLastName(lastName);
          s.setEmail(email);
          s.setPersonID(personId);
          em.persist(s);
          return s;
       }
       
       if (personType.equalsIgnoreCase("PROFESSOR"))
       {
           Professor p = new Professor();
           p.setAddress(address);
           p.setDepartmentName(deptName);
           p.setFirstName(firstName);
           p.setLastName(lastName);
           p.setPersonID(personId);
           p.setEmail(email);
           em.persist(p);
           return p;
           
       }
       
       if (personType.equalsIgnoreCase("REGISTRAR"))
       {
           Registrar r = new Registrar();
           r.setAddress(address);
           r.setDepartmentName(deptName);
           r.setFirstName(firstName);
           r.setLastName(lastName);
           r.setPersonID(personId);
           r.setEmail(email);
           em.persist(r);
           return r;
       }
       
       if (personType.equalsIgnoreCase("ADMIN"))
       {
           Admin a = new Admin();
           a.setAddress(address);
           a.setDepartmentName(deptName);
           a.setFirstName(firstName);
           a.setLastName(lastName);
           a.setPersonID(personId);
           a.setEmail(email);
           em.persist(a);
           return a;
       }
       
       if (personType.equalsIgnoreCase("DBADMIN"))
       {
           DBAdmin d = new DBAdmin();
           d.setAddress(address);
           d.setDepartmentName(deptName);
           d.setFirstName(firstName);
           d.setLastName(lastName);
           d.setPersonID(personId);
           d.setEmail(email);
           em.persist(d);
           return d;
       }
       // we should not get here
       return null;
    }

    public void editPerson(String personType, String strAddress, String city, String state, String zip, String major, String deptName, String firstName, String lastName, String email, Person person) {
        
       Address address = new Address();
       address.setStreetAddr(strAddress);
       address.setCity(city);
       address.setState(state);
       address.setZipCode(zip);
            
       if (personType.equalsIgnoreCase("STUDENT"))
       {
          Student s = (Student) person;
          s.setAddress(address);
          s.setMajor(major);
          s.setFirstName(firstName);
          s.setLastName(lastName);
          s.setEmail(email);
          em.merge(s);
 
       
       }
       
       if (personType.equalsIgnoreCase("PROFESSOR"))
       {
           Professor p = (Professor) person;
           p.setAddress(address);
           p.setDepartmentName(deptName);
           p.setFirstName(firstName);
           p.setLastName(lastName);
           p.setEmail(email);
           em.merge(p);
           
       }
       
       if (personType.equalsIgnoreCase("REGISTRAR"))
       {
           Registrar r = (Registrar) person;
           r.setAddress(address);
           r.setDepartmentName(deptName);
           r.setFirstName(firstName);
           r.setLastName(lastName);
           r.setEmail(email);
           em.merge(r);
       }
       
       if (personType.equalsIgnoreCase("ADMIN"))
       {
           Admin a = (Admin) person;
           a.setAddress(address);
           a.setDepartmentName(deptName);
           a.setFirstName(firstName);
           a.setLastName(lastName);
           a.setEmail(email);
           em.merge(a);
           
       }
       
       if (personType.equalsIgnoreCase("DBADMIN"))
       {
           DBAdmin d = (DBAdmin) person;
           d.setAddress(address);
           d.setDepartmentName(deptName);
           d.setFirstName(firstName);
           d.setLastName(lastName);
           d.setEmail(email);
           em.merge(d);
           
       }
    }
}
